﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace DBFile.SqlServerProvider
{
	public static class SqlServerHelper
	{
		private readonly static string VersionSql = "SELECT @@version";

		internal static string GetVersion(SqlConnection openConnection)
		{
			string versionNumber = null;

			SqlCommand cmd = new SqlCommand(VersionSql, openConnection);
			cmd.CommandType = CommandType.Text;

			object result = cmd.ExecuteScalar();

			versionNumber = result.ToString();

			if (versionNumber == null)
			{
				throw new ApplicationException("Unknow Version");
			}

			return versionNumber;
		}

		public static bool IsSql2000(SqlConnection openConnection)
		{
			return GetVersion(openConnection).IndexOf(" - 8.") >= 0;
		}

		public static bool IsSql2005(SqlConnection openConnection)
		{
			return GetVersion(openConnection).IndexOf(" - 9.") >= 0;
		}

        public static bool IsSql2008(SqlConnection openConnection)
        {
            return GetVersion(openConnection).IndexOf(" - 10.") >= 0;
        }

		public static SqlDbType GetFileColumnType(SqlConnection openConnection)
		{
			if (IsSql2000(openConnection))
			{
				return SqlDbType.Image;
			}
			else
			{
				return SqlDbType.VarBinary;
			}
		}
	}
}
